﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using KPWeb.Class;
using KPWeb.Data;
using KPWeb.Function;
using KPWeb.Reports;
using MSExcel = Microsoft.Office.Interop.Excel;
//using Microsoft.Reporting.WebForms;


namespace KPWeb.Dyzury
{

    public partial class DokumentPlanuDyzurow : KPPage
    {
        private readonly string[] MonthNames = { "Styczeń", "Luty", "Marzec", "Kwiecień", "Maj", "Czerwiec", "Lipiec", "Sierpień", "Wrzesień", "Październik", "Listopad", "Grudzień" };
        private short RokPlany
        {
          get
            {
                if (ViewState["RokPlany"] == null )
                      if ( DateTime.Now.Month!=12) ViewState["RokPlany"] = DateTime.Now.Year;
                          else ViewState["RokPlany"] = DateTime.Now.AddYears(1).Year;
                return short.Parse(ViewState["RokPlany"].ToString());
            }
            set
            {
                ViewState["RokPlany"] = value;

            }

        }
        private void komunikatError(int kod)//, int iT, int iID)
        {
            fError.Visible = true;
            switch (kod)
            {
                case -1: Error1.Visible = true;
                    break;
                case -2: Error2.Visible = true;
                    break;
                case -3: Error3.Visible = true;
                    break;
                case -4: Error4.Visible = true;
                    break;
                case -5: Error5.Visible = true;
                    break;
                default: fError.Visible = false;
                    break;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            fError.Visible = false;
            Error1.Visible = false;
            Error2.Visible = false;
            Error3.Visible = false;
            Error4.Visible = false;
            Error5.Visible = false;
            if (!IsPostBack)
            {


                dTypDyzuru.Items.Clear();
                foreach (vLex_TypDyzuru u in DyzuryK.GetListaTypDyzuru().OrderBy(a => a.KodDyzuru))
                {
                    this.dTypDyzuru.Items.Add(new ListItem() { Text = u.NazwaDyzuru + " (" + u.KodDyzuru + ")", Value = u.TypDyzuruID.ToString() });
                }
                dTypDyzuru.SelectedValue = Stale.dyzurInic;

                dYear.Items.Clear();
                lYear.Items.Clear();
                foreach (short item in Kalendarz.GetYearGdzieWpisanoSwieta())
                {
                    this.dYear.Items.Add(item.ToString());
                    if (RokPlany >= short.Parse(item.ToString())) this.lYear.Items.Add(item.ToString());
                }

                dYear.SelectedValue = RokPlany.ToString();
                lYear.SelectedValue = RokPlany.ToString();
                dMonth.Items.Clear();
                for (int i = 0; i < 12; i++)
                {
                    dMonth.Items.Add(new ListItem()
                    {
                        Text = MonthNames[i],
                        Value = (i + 1).ToString()
                    });
                 }

                dMonth.SelectedValue = DateTime.Now.AddMonths(1).Month.ToString();
                tWypelnianieOdData.Text = String.Format("{0:yyyy-MM-dd}",new DateTime(RokPlany, DateTime.Now.Month, DateTime.Now.Day));

                tWypelnianieDoData.Text = String.Format("{0:yyyy-MM-dd}", new DateTime(RokPlany, DateTime.Now.Month, DateTime.DaysInMonth(RokPlany, DateTime.Now.Month) - 1));
                hJednostkaID.Value = Current.JednostkaID;
            }

        }

     protected void PlanyDyzurow_Click(object sender, System.EventArgs e)
        {
            DateTime dd1;
            DateTime dd2;
            int iMonth = int.Parse(dMonth.SelectedValue);
            DateTime dOd=new DateTime(RokPlany, iMonth, 1);
            if (!DateTime.TryParse(tWypelnianieOdData.Text, out dd1) || !DateTime.TryParse(tWypelnianieDoData.Text, out dd2))
            {
                komunikatError(-5);
                return;
            }
            if (dd1.Date > dd2.Date || dd2.Date < DateTime.Now.Date )
         {
             komunikatError(-5);
             return;
         }
         
            int outProc = DyzuryK.InsertPlanyDyzurow(
               int.Parse(dTypDyzuru.SelectedValue),
               tOpis.Text,
               dOd,
               new DateTime(RokPlany, iMonth, DateTime.DaysInMonth(RokPlany, iMonth)),
               int.Parse(Current.JednostkaID),dd1,dd2,
                int.Parse(Current.OsobaID));
            if (outProc < 0)
            {
                komunikatError(outProc);
                return;
            }
            this.ListView1.DataBind();
        }

        protected void urlopRLBRoboczy_Click(object sender, System.EventArgs e)
        {
            string sTemp = ((KPWeb.Kontrolki.RaportLinkButton)sender).CommandArgument.ToString();
            for (int i = sTemp.Length; i < Stale.countDigitsInBarCode - 2; i++)
            {
                sTemp = "0" + sTemp;
            }
            sTemp = Stale.idWU + sTemp;
            RaportyPDF.RaportWniosekUrlopowyBarCodePDF(int.Parse(sTemp), 0);
        }
        protected void urlopRLBOryginal_Click(object sender, System.EventArgs e)
        {

            string sTemp = ((KPWeb.Kontrolki.RaportLinkButton)sender).CommandArgument.ToString();

            for (int i = sTemp.Length; i < Stale.countDigitsInBarCode - 2; i++)
            {
                sTemp = "0" + sTemp;
            }

            sTemp = Stale.idWU + sTemp;

            RaportyPDF.RaportWniosekUrlopowyBarCodePDF(int.Parse(sTemp), 1);


        }

       
  protected void btnKasujClick(object sender, EventArgs e)
        {

            int outProc = DyzuryK.DeletePlanyDyzurow(
                           int.Parse(((Button)sender).CommandArgument.ToString()));
            if (outProc < 0)
            {
                komunikatError(outProc);
                return;
            }
        
            this.ListView1.DataBind();
        
        }
  protected void btnZapiszDateWypelnianieDoClick(object sender, EventArgs e)
  {
      DateTime dd1 = DateTime.Parse(((Label)((Button)sender).Parent.FindControl("lWypelnianieOdData")).Text);
      DateTime dd2;

      if (!DateTime.TryParse(((TextBox)((Button)sender).Parent.FindControl("lWypelnianieDoData")).Text, out dd2))
      {
          komunikatError(-5);
          return;
      }
      if (dd1.Date > dd2.Date || dd2.Date < DateTime.Now.Date)
      {
          komunikatError(-5);
          return;
      }
      int outProc = DyzuryK.UpdateWypelnianieDoPlanyDyzurow(
          int.Parse(((Button)sender).CommandArgument.ToString()),dd2,
          int.Parse(Current.OsobaID));
      if (outProc < 0)
      {
          komunikatError(outProc);
          return;
      }

      this.ListView1.DataBind();

  } 
  protected void btnZaakceptujClick(object sender, EventArgs e)
  {

      int outProc = DyzuryK.UpdatePlanyDyzurow(
                     int.Parse(((Button)sender).CommandArgument.ToString()), 0, 1,
          int.Parse(Current.OsobaID));
      if (outProc < 0)
      {
          komunikatError(outProc);
      }
      this.ListView1.DataBind();
 
  }
  protected void btnCofnijClick(object sender, EventArgs e)
  {

      int outProc = DyzuryK.UpdatePlanyDyzurow(
                     int.Parse(((Button)sender).CommandArgument.ToString()), 1, 0,
          int.Parse(Current.OsobaID));
      if (outProc < 0)
      {
          komunikatError(outProc);
      }
      this.ListView1.DataBind();

  }
  //protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
  //{
  //    //string sTemp = ((Label)e.Item.FindControl("Zastepstwo")).Text;

  //    //((Label)e.Item.FindControl("Zastepstwo")).Text = FunctionGlobal.UppercaseFirstWords(sTemp);
  //}

  protected void lKody_Click(object sender, EventArgs e)
  {
      Server.Transfer("~/Slowniki/TypyDyzurow.aspx");


  }

    }
}